﻿using ChoMy.Models;
using ChoMy.Models.Data;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ChoMy.Controllers
{
    [Authorize]
    public class ReceiverController : Controller
    {
        private ChoMyContext db = new ChoMyContext();

        public ActionResult Index()
        {
            return RedirectToAction("Create");
        }

        [ChildActionOnly]
        public ActionResult List()
        {
            List<Receiver> receiverList = db.Receivers
                .Where(x => x.CreatedBy == User.Identity.Name)
                .OrderByDescending(x => x.DateCreated)
                .ToList();

            return PartialView("_List", receiverList);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(ReceiverViewModel model)
        {
            if (ModelState.IsValid)
            {
                Receiver receiverItem = new Receiver();
                receiverItem.Name = model.Name;
                receiverItem.Address = model.Address;
                receiverItem.Email = model.Email;
                receiverItem.Phone = model.Phone;

                db.Receivers.Add(receiverItem);
                db.SaveChanges();

                return RedirectToAction("Index", "Payment", new { receiverId = receiverItem.Id });
            }

            return View(model);
        }

        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("BadRequest", "Error");
            }

            Receiver receiverItem = db.Receivers.Find(id);

            if (receiverItem == null || receiverItem.CreatedBy != User.Identity.Name)
            {
                return RedirectToAction("NotFound", "Error");
            }

            ReceiverViewModel model = new ReceiverViewModel();
            model.Id = receiverItem.Id;
            model.Name = receiverItem.Name;
            model.Email = receiverItem.Email;
            model.Address = receiverItem.Address;
            model.Phone = receiverItem.Phone;

            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ReceiverViewModel model)
        {

            if (ModelState.IsValid)
            {
                Receiver receiverItem = db.Receivers.Find(model.Id);
                receiverItem.Name = model.Name;
                receiverItem.Address = model.Address;
                receiverItem.Email = model.Email;
                receiverItem.Phone = model.Phone;
                receiverItem.DateModified = DateTime.Now;

                db.Entry(receiverItem).State = EntityState.Modified;
                db.SaveChanges();

                return RedirectToAction("Index", "Payment", new { receiverId = receiverItem.Id });
            }

            return View(model);
        }

        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("BadRequest", "Error");
            }

            Receiver receiverItem = db.Receivers.Find(id);

            if (receiverItem == null || receiverItem.CreatedBy != User.Identity.Name)
            {
                return RedirectToAction("NotFound", "Error");
            }

            return View(receiverItem);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id)
        {
            Receiver receiverItem = db.Receivers.Find(id);

            if (receiverItem.CreatedBy != User.Identity.Name)
            {
                return RedirectToAction("NotFound", "Error");
            }

            db.Receivers.Remove(receiverItem);
            db.SaveChanges();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }

            base.Dispose(disposing);
        }
    }
}